package com.example.gaoyang.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.gaoyang.constant.Constant;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;

public class DateStringUtil {

    /**
     * 判断字符串是否为yyyy-MM-dd HH:mm:ss格式的日期时间字符串
     * @param dateTimeStr
     * @return
     */
    public static boolean isValidDateTime(String dateTimeStr) {
        if (StringUtils.isBlank(dateTimeStr)) {
            return true;
        }
        // 定义两种日期时间格式
        String[] formats = {"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"};
        for (String format : formats) {
            try {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
                if (format.equals("yyyy-MM-dd")) {
                    LocalDate.parse(dateTimeStr, formatter);
                } else {
                    LocalDateTime.parse(dateTimeStr, formatter);
                }
                return true;
            } catch (DateTimeParseException e) {
                // 如果解析失败，继续尝试下一个格式
            }
        }
        // 如果所有格式都解析失败，返回 false
        return false;
    }

    /**
     * 判断第一个日期时间是否早于第二个日期时间
     * @param firstDateTimeStr
     * @param secondDateTimeStr
     * @return
     */
    public static boolean isFirstTimeBeforeSecond(String firstDateTimeStr, String secondDateTimeStr) {
        // 开始时间和结束时间只要有一个为空就返回true，不再进行判断
        if (StringUtils.isBlank(firstDateTimeStr) || StringUtils.isBlank(secondDateTimeStr)) {
            return true;
        }
        if (!isValidDateTime(firstDateTimeStr) || !isValidDateTime(secondDateTimeStr)) {
            return false;
        }
        DateTimeFormatter fullFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter dateOnlyFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime firstDateTime;
        LocalDateTime secondDateTime;
        try {
            if (firstDateTimeStr.contains(":")) {
                firstDateTime = LocalDateTime.parse(firstDateTimeStr, fullFormatter);
            } else {
                LocalDate date = LocalDate.parse(firstDateTimeStr, dateOnlyFormatter);
                firstDateTime = date.atStartOfDay();
            }
            if (secondDateTimeStr.contains(":")) {
                secondDateTime = LocalDateTime.parse(secondDateTimeStr, fullFormatter);
            } else {
                LocalDate date = LocalDate.parse(secondDateTimeStr, dateOnlyFormatter);
                secondDateTime = date.atStartOfDay();
            }
            return firstDateTime.isBefore(secondDateTime);
        } catch (DateTimeParseException e) {
            // 如果解析失败，返回 false
            return false;
        }
    }

    /**
     * 计算两个日期之间的时间差
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @param format    返回格式，支持 "HH:mm:ss" 或 "mm:ss"
     * @return 时间差字符串
     */
    public static String calculateTimeDifference(Date startDate, Date endDate, String format) {
        if (startDate == null || endDate == null) {
            return "00:00:00";
        }
        long diffMillis = endDate.getTime() - startDate.getTime();
        long seconds = diffMillis / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;
        seconds %= 60;
        minutes %= 60;
        switch (format) {
            case "HH:mm:ss":
                return String.format("%02d:%02d:%02d", hours, minutes, seconds);
            case "mm:ss":
                return String.format("%02d:%02d", minutes + (hours * 60), seconds);
            default:
                throw new IllegalArgumentException("Unsupported format: " + format);
        }
    }

    /**
     * 将yyyy-MM-dd HH:mm:ss格式的字符串转换为Date类型
     * @param dateStr 日期字符串
     * @return Date对象
     */
    public static Date parseStringToDate(String dateStr) {
        if (StringUtils.isEmpty(dateStr)) {
            return null;
        }
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Constant.YYYY_MM_DD_HH_MM_SS);
            LocalDateTime localDateTime = LocalDateTime.parse(dateStr, formatter);
            return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        } catch (Exception e) {
            return null;
        }
    }

}
